Day 7 - Sequences and counting
– A coding sequence cannot be revised once it’s been established.
Blade Runner (1982)
Today we will relax a bit, lately we have been doing a lot, so it’s time to learn something easy
and straightforward. So, we will discuss distributed consensus in a network system. No, I’m just
joking, that is actually far from being simple. OK, let’s pick another topic: what do you think about
sequences? I find them extremely useful, for example when you need to rename files and you want
to give them a proper order.
Whatever the use, Unix provides a very nice command to create sequences of integers, aptly named
seq.
Note: I’m always unsure about the proper pronunciation of seq. As an Italian, I tend to pronounce it
as “sec”, but if you want to go for something else I understand you. You can spell it, with the benefit
that if you use the right tone you can sound like a field officer during a battle.
The basic usage is very simple, as you just need to specify a number grater than 1 and seq will output
all integers between 1 and that number.
$ seq 10
Now, this is useful, but sometimes you need to count between two specific numbers, for example
because you want to include 0, or because you simply need to start from 42, which is always a good
initial value. In this case you just give seq the first and the last number
$ seq 4 11
We are not restricted to positive numbers, however. If you give seq a negative starting point it will
happily digest it
$ seq -4 6
Unfortunately, the opposite doesn’t work out of the box. If you try to execute seq 4 -5 you won’t
get any output. To perform a reverse count you need to specify the interval between the steps, which
is 1 by default